package com.sunwayhorizo.health.platform.security.dao.impl;

import java.util.List;

import javax.ejb.Stateless;

import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.platform.base.BaseManager;
import com.sunwayhorizo.health.platform.common.PlatformConstant;
import com.sunwayhorizo.health.platform.security.dao.SecurityResourceDao;
import com.sunwayhorizo.health.platform.security.persistent.SecurityPermissionRole;
import com.sunwayhorizo.health.platform.security.persistent.SecurityResource;

@Stateless
@Name("SecurityResourceDao")
public class SecurityResourceImpl extends BaseManager implements SecurityResourceDao{

	@Override
	public SecurityResource newSecurityResource(SecurityResource resource) {
		super.save(resource);
		return resource;
	}
	
	public void deleteSecurityResource(List<String> list) {
		super.delete(SecurityResource.class,list);
	}
	
	@Override
	public SecurityResource updateSecurityResource(SecurityResource resource) {
		super.update(resource);
		return resource;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityResource> findSecurityResourceList() {
		return (List<SecurityResource>)super.entityManager2.createNamedQuery("SecurityResource.findAll").getResultList();
	}

	@SuppressWarnings("unchecked")
    @Override
    public List<SecurityResource> findSecurityResourceListWithOutRootNode() {
	    return super.entityManager2.createQuery("select r from SecurityResource r where r.resourceId != :rootId").setParameter("rootId", PlatformConstant.ROOT_RESOURCE).getResultList();
    }
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityResource> findSecurityResourceListWithFatherResource() {
		return super.entityManager2.createQuery("select distinct sr from SecurityResource sr left join fetch sr.fatherResource").getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityPermissionRole> findSecurityResourceListByRoleId(String roleId) {
		String jpql = "select distinct spr from SecurityPermissionRole spr left join spr.permission sp left join fetch sp.resource left join fetch sp.operation " +
					  "where spr.role.roleId=:roleId ";
		return super.entityManager2.createQuery(jpql).setParameter("roleId", roleId).getResultList();
	}
	
	@Override
	public SecurityResource findSecurityResourceDetailById(String resourceId) {
		return super.find(SecurityResource.class, resourceId);
	}

	@Override
	public SecurityResource findSecurityResourceDetailByName(String resourceName) {
		return (SecurityResource)super.entityManager2.createQuery("select sr from SecurityResource sr where sr.resourceName = :resourceName").setParameter("resourceName", resourceName).getSingleResult();
	}

	@Override
	public Long findSecurityResourceListSizeByResourceId(String resourceId) {
        String jpql = "select count(distinct sp.permissionId ) from SecurityPermission sp where sp.resource.resourceId =:resourceId ";
        return (Long)super.entityManager2.createQuery(jpql).setParameter("resourceId", resourceId).getSingleResult();
    }
	
	@Override
    public Long findSecurityResourceListSizeNotExistsInPermissionRoleByResourceId(String resourceId) {
        String jpql = "select count(distinct sp.permissionId ) from SecurityPermission sp join sp.resource sr where sr.resourceId =:resourceId " +
                      " and not exists("+
                      "     select spr.permissionRoleId from SecurityPermissionRole spr where spr.permission.permissionId = sp.permissionId "+
                      " )";
        return (Long)super.entityManager2.createQuery(jpql).setParameter("resourceId", resourceId).getSingleResult();
    }
	
	@Override
    public Long findSecurityResourceListSizeNotExistsInPermissionAccountByResourceId(String resourceId) {
        String jpql = "select count(distinct sp.resource.resourceId) from SecurityPermission sp where sp.resource.resourceId =:resourceId " +
                      " and not exists("+
                      "     select spa.permissionAccountId from SecurityPermissionAccount spa where spa.permission.permissionId = sp.permissionId "+
                      " )";
        return (Long)super.entityManager2.createQuery(jpql).setParameter("resourceId", resourceId).getSingleResult();
    }
	
	@Override
    public Long findSecurityResourceListSizeNotExistsInAccountRolePermissionByResourceId(String resourceId) {
        String jpql = "select count(distinct sp.resource.resourceId) from SecurityPermission sp where sp.resource.resourceId =:resourceId " +
                      " and not exists("+
                      "     select sarp.arpId from SecurityAccountRolePermission sarp where sarp.permission.permissionId = sp.permissionId "+
                      " )";
        return (Long)super.entityManager2.createQuery(jpql).setParameter("resourceId", resourceId).getSingleResult();
    }
}
